﻿// ScalarTest.cs
//
// Copyright 2008-2011 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace YaTools.Yaml.UnitTests
{
    /// <summary>
    ///This is a test class for ScalarTest and is intended
    ///to contain all ScalarTest Unit Tests
    ///</summary>
    [TestClass]
    public class ScalarTest
    {
        /// <summary>
        ///Gets or sets the test dumper which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion

        /// <summary>
        ///A test for IsNonSpace
        ///</summary>
        [TestMethod]
        [Ignore] // Questionable how the YAML spec defines it...
        public void IsNonSpaceTest()
        {
            bool failed = false;
            for (int i = 0; i < 0xFFFF; i++)
            {
                var c = (char) i;
                bool expected = !Char.IsControl(c) && !Char.IsWhiteSpace(c)
                                && !Char.IsSurrogate(c);
                bool actual = Scalar.IsNonSpace(c);

                if (expected != actual)
                {
                    Console.WriteLine(
                        "IsNotSpace incorrectly returned {0} for U+{1:X4}",
                        actual, i);
                    failed = true;
                }
            }
            Assert.IsFalse(failed, "See output for failing cases");
        }

        /// <summary>
        ///A test for IsPlainKey
        ///</summary>
        [TestMethod]
        public void IsPlainKeyTest()
        {
            TestPlainKey("one", true, true);
            TestPlainKey("one", false, true);
            TestPlainKey("::vector", false, true);
            TestPlainKey("::vector", true, true);
            TestPlainKey("-123", false, true);
            TestPlainKey("-123", true, true);
            TestPlainKey("http://example.com/foo#bar", false, true);
            TestPlainKey("http://example.com/foo#bar", true, true);
        }

        private static void TestPlainKey(string value, bool inKey, bool expected)
        {
            bool actual = Scalar.IsPlainKey(value, inKey);
            if (expected != actual)
                Assert.Fail(
                    "Scalar.IsPlainKey incorrectly returned {0} for {1}",
                    actual, value);
        }
    }
}